package com.example.pets_java.service;

import com.example.pets_java.mapper.CommentMapper;
import com.example.pets_java.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;

    public Comment getCommentById(Long id) {
        return commentMapper.getCommentById(id);
    }


    public List<Comment> selectCommentList(Comment comment){
        comment.setMainId(0L);
        //根据文章id获取评论列表
        List<Comment> slCommentList = commentMapper.selectCommentList(comment);

        for (Comment commentItem:slCommentList) {

            //查询子评论
            Comment childParams = new Comment();
            childParams.setMainId(commentItem.getCId());
            List<Comment> childlCommentsList = commentMapper.selectCommentList(childParams);

            commentItem.setChildren(childlCommentsList);

        }

        return slCommentList;
    }


    public void insertComment(Comment comment){
        commentMapper.insertComment(comment);
    }


}
